package com.youtui.manager.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youtui.common.constant.ResultMessage;
import com.youtui.common.constant.SystemConstant;
import com.youtui.common.service.base.impl.BaseServiceImpl;
import com.youtui.common.util.DateUtils;
import com.youtui.common.util.NextIdUtil;
import com.youtui.common.util.ResultUtil;
import com.youtui.manager.entity.WithdrawalEntity;
import com.youtui.manager.mapper.WithdrawalMapper;
import com.youtui.manager.model.Account;
import com.youtui.manager.model.AccountInfo;
import com.youtui.manager.model.Withdrawal;
import com.youtui.manager.service.IAccountInfoService;
import com.youtui.manager.service.IAccountService;
import com.youtui.manager.service.IWithdrawalService;

@Service
public class WithdrawalServiceImpl extends BaseServiceImpl implements IWithdrawalService {

	@Autowired
	WithdrawalMapper withdrawalMapper;
	@Autowired
	IAccountService accountService;

	@Autowired
	IAccountInfoService accountInfoService;

	@Override
	public PageInfo<WithdrawalEntity> findWithdrawalPageList(String mobile, String bankNo, Date applyDate, int page,
			int rows) {
		PageHelper.startPage(page, rows);
		List<WithdrawalEntity> list = findWithdrawalList(null, mobile, bankNo, applyDate);
		PageInfo<WithdrawalEntity> pageinfo = new PageInfo<>(list);
		return pageinfo;
	}

	public PageInfo<WithdrawalEntity> findWithdrawalPageList(Long devId, int page, int rows) {
		PageHelper.startPage(page, rows);
		List<WithdrawalEntity> list = findWithdrawalList(devId, null, null, null);
		PageInfo<WithdrawalEntity> pageinfo = new PageInfo<>(list);
		return pageinfo;
	}

	@Override
	public List<WithdrawalEntity> findWithdrawalList(Long devId, String mobile, String bankNo, Date applyDate) {
		Map<String, Object> map = new HashMap<String, Object>();

		if (devId != null) {
			map.put("devId", devId);
		}

		if (StringUtils.isNotBlank(mobile)) {
			map.put("mobile", mobile);
		}
		if (StringUtils.isNotBlank(bankNo)) {
			map.put("bankNo", "%" + bankNo + "%");
		}
		if (applyDate != null) {
			map.put("applyDate", applyDate);
			map.put("end_applyDate", DateUtils.addDays(applyDate, 1));
		}
		List<WithdrawalEntity> list = withdrawalMapper.selectWithdrawalEntityPageList(map);
		return list;
	}

	@Override
	public WithdrawalEntity loadWithdrawal(Long id) {
		if (id == null) {
			return null;
		}
		return (WithdrawalEntity) withdrawalMapper.selectByPrimaryKey(id);
	}

	@Override
	public ResultMessage checkWithdrwal(Long id, Integer auditType, String auditInfo) {

		if (id == null || auditType == null) {
			return ResultUtil.result(ResultUtil.code_fail, "请求参数错误");
		}

		Withdrawal withdrawal = withdrawalMapper.selectByPrimaryKey(id);
		if (withdrawal == null) {
			return ResultUtil.result(ResultUtil.code_fail, "提现信息不存在");
		}

		if (withdrawal.getStatus() != SystemConstant.ApplyStatusApply) {
			return ResultUtil.result(ResultUtil.code_fail, "当前非待审核状态");
		}

		if (StringUtils.isBlank(auditInfo)) {
			auditInfo = "";
		} else {
			auditInfo += ";";
		}
		if (auditType == SystemConstant.AuditTypeConfirm) {
			withdrawal.setStatus(SystemConstant.ApplyStatusChecked); //
		}
		if (auditType == SystemConstant.AuditTypeRefuse) {
			withdrawal.setStatus(SystemConstant.ApplyStatusCancel); //
			//
			Integer result = accountService.updateAvailWallet(withdrawal.getAccountId(), withdrawal.getApplyMoney());
			if (result == ResultUtil.code_fail) {
				return ResultUtil.result(ResultUtil.code_fail, "账户余额退款失败");
			}
		}
		withdrawal.setDealTime(new Date());
		withdrawal.setInfo(auditInfo);
		withdrawalMapper.updateByPrimaryKeySelective(withdrawal);

		return ResultUtil.result(ResultUtil.code_success, "提现确认操作成功");
	}

	@Override
	public ResultMessage confirmWithdrawal(Long id, Integer auditType, String auditInfo) {

		if (id == null || auditType == null) {
			return ResultUtil.result(ResultUtil.code_fail, "请求参数错误");
		}

		Withdrawal withdrawal = withdrawalMapper.selectByPrimaryKey(id);
		if (withdrawal == null) {
			return ResultUtil.result(ResultUtil.code_fail, "提现信息不存在");
		}

		if (withdrawal.getStatus() != SystemConstant.ApplyStatusChecked) {
			return ResultUtil.result(ResultUtil.code_fail, "当前非待确认状态");
		}

		if (StringUtils.isBlank(auditInfo)) {
			auditInfo = "";
		}

		withdrawal.setStatus(SystemConstant.ApplyStatusConfirm); // 确认
		withdrawal.setDealTime(new Date());
		withdrawal.setInfo(withdrawal.getInfo() + auditInfo);
		withdrawalMapper.updateByPrimaryKeySelective(withdrawal);

		return ResultUtil.result(ResultUtil.code_success, "提现确认操作成功");
	}

	@Override
	public Integer add(Account account, BigDecimal applymoney) {

		if (account == null) {
			return 0;
		}
		AccountInfo accountInfo = accountInfoService.findAccountInfoByDevId(account.getDevId());
		
		Withdrawal withdrawal = new Withdrawal();
		withdrawal.setId(NextIdUtil.getNextId());
		withdrawal.setDevId(account.getDevId());
		withdrawal.setAccountId(account.getId());
		withdrawal.setActInfoId(accountInfo.getId());
		withdrawal.setAvailWallet(account.getAvailWallet());
		withdrawal.setStatus(SystemConstant.status_effect);
		withdrawal.setApplyMoney(applymoney);
		withdrawal.setApplyTime(new Date());
		return withdrawalMapper.insert(withdrawal);
	}

}
